﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WAU.DTOs.CommodityModule;
using WAU.Models.CommodityModels;
using WAU.Models.Framework;
using WAU.Services;

namespace WAU.Controllers
{
    /// <summary>
    /// 商品 Controller
    /// </summary>    
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Authorize]
    public class CommodityController : EssentialController
    {
        private readonly CommodityService commodityService;

        /// <summary>
        /// ctor
        /// </summary>
        public CommodityController(CommodityService commodityService)
        {
            this.commodityService = commodityService;
        }

        /// <summary>
        /// 获取商品列表（管理端）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCommodityList(ListCommodityDto dto)
        {
            var result = await commodityService.GetCommodityList(dto);
            return Ok(new Result
            {
                Success = true,
                Data = result.DataList,
                Total = result.Total
            });
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> AddCommodity(AddCommodityDto dto)
        {
            ExecResult execResult = await commodityService.AddCommodity(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = execResult.Success,
                Msg = execResult.ErrorMsg
            });
        }

        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCommodityDetail(DetailCommodityDto dto)
        {
            ExecResult<CommodityDetailSco> execResult = await commodityService.GetCommodityDetail(dto.Id);
            if(execResult.Success)
            {
                return Ok(new Result
                {
                    Success = execResult.Success,
                    Msg = execResult.ErrorMsg,
                    Data = execResult.Data
                });
            }
            else
            {
                return StatusCode(500, new Result
                {
                    Success = execResult.Success,
                    Msg = execResult.ErrorMsg,
                    Data = execResult.Data
                });
            }
            
        }

        /// <summary>
        /// 编辑商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditCommodityMain(EditCommodityDto dto)
        {
            ExecResult execResult = await commodityService.EditCommodityMain(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = execResult.Success,
                Msg = execResult.ErrorMsg
            });
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteCommodity(DeleteCommodityDto dto)
        {
            ExecResult result = await commodityService.DeleteCommodity(CurrentUserId, dto.Id);

            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }


        /// <summary>
        /// 修改商品轮播图 Dto
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditCommoditySlideShowImages(EditCommoditySlideShowImagesDto dto)
        {
            ExecResult result = await commodityService.EditCommoditySlideShowImages(
                                    CurrentUserId, dto.CommodityId, dto.SlideShowImageItems);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 新增/编辑商品 SKU
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditCommoditySKU(EditCommoditySKUDto dto)
        {
            ExecResult result = await commodityService.EditCommoditySKU(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 删除商品 SKU
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteCommoditySKU(DeleteCommoditySKUDto dto)
        {
            ExecResult result = await commodityService.DeleteCommoditySKU(CurrentUserId, dto.Id);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 上传商品主图
        /// </summary>
        /// <param name="_"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadCommodityMainImage([FromForm] IFormFile _)
        {
            return EssentialUploadFile("MainImage");
        }

        /// <summary>
        /// 上传商品轮播图
        /// </summary>
        /// <param name="_"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadCommoditySlideShowImage([FromForm] IFormFile _)
        {
            return EssentialUploadFile("CommodityImages");
        }

        /// <summary>
        /// 上传商品详情中的图片
        /// </summary>
        /// <param name="_"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadCommodityDetailImage([FromForm] IFormFile _)
        {
            return EssentialUploadFiles("CommodityDetailImage");
        }

        /// <summary>
        /// 上传商品 SKU 图片
        /// </summary>
        /// <param name="_"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadCommoditySKUImage([FromForm] IFormFile _)
        {
            return EssentialUploadFile("CommoditySKUImage");
        }


        /// <summary>
        /// 获取商品分类列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAllCommodityCategoryList(ListCommodityCategoryDto dto)
        {
            List<CommodityCategory> resultList = await commodityService.GetAllCommodityCategoryList(dto);
            return Ok(new Result
            {
                Success = true,
                Data = resultList,
            });
        }

        /// <summary>
        /// 获取商品分类列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCommodityCategoryList(ListCommodityCategoryDto dto)
        {
            QueryResult<CommodityCategory> queryResult = await commodityService.GetCommodityCategoryList(dto);
            return Ok(new Result
            {
                Success = true,
                Data = queryResult.DataList,
                Total = queryResult.Total
            });
        }

        /// <summary>
        /// 编辑商品分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditCommodityCategory(EditCommodityCategoryDto dto)
        {
            ExecResult result = await commodityService.EditCommodityCategory(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 删除商品分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteCommodityCategory(DeleteCommodityCategoryDto dto)
        {
            ExecResult result = await commodityService.DeleteCommodityCategory(CurrentUserId, dto.Id);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }
    }
}
